//
//  BaseViewModel.swift
//  SwiftDemo
//
//  Created by 李国卿 on 2020/1/17.
//  Copyright © 2020 guoqing.li. All rights reserved.
//

import UIKit

class BaseViewModel {
    lazy var anchorGroups : [AnchorGroupModel] = [AnchorGroupModel]()
}

extension BaseViewModel{
    func loadAnchorModel(isGroupData: Bool, URLString: String, parameters:[String : Any]? = nil, finishCallBack: @escaping () -> ()){
        NetWorkTools.requestData(type: .get, URLString: URLString, parameters: parameters) { (result) in
            //1 获取字典数据
            guard let resultDict = result as? [String : Any] else { return }
            guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }
            //2 判断是否为分组数据
            if isGroupData {
                // 2.1 字典转模型
                for dict in dataArray {
                    self.anchorGroups.append(AnchorGroupModel(dic: dict))
                }
            }else{
                // 2.1 创建组
                let group = AnchorGroupModel()
                // 2.2 字典转模型
                for dict in dataArray {
                    group.anchors.append(AnchorModel(dic: dict))
                }
                // 2.3 将 group 添加到 AnchorGroupModel 中
                self.anchorGroups.append(group)
            }
            finishCallBack()
        }
    }
}
